package com.perselab.recipe.model;

import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@Entity
@Table(name="category")
public class Category extends AbstractModel implements Auditable {
	private static final long serialVersionUID = 4388725798661171935L;

	private Long id;
	private String name;
	private Date creationDate;
	private Date modificationDate;
	private int status;

	private Set<Recipe> recipes;

	/* (non-Javadoc)
	 * @see com.perselab.recipe.model.Model#getId()
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return this.id;
	}

	@Column
	public String getName() {
		return name;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "category_recipe", joinColumns = { @JoinColumn(name = "category_id") }, inverseJoinColumns = @JoinColumn(name = "recipe_id"))
	@OrderBy("name")
	public Set<Recipe> getRecipes() {
		return recipes;
	}
	
	@Override
	@Column(name="creation_date")
	public Date getCreationDate() {
		return this.creationDate;
	}

	@Override
	@Column(name="modification_date")
	public Date getModificationDate() {
		return this.modificationDate;
	}
	
	@Override
	@Column
	public int getStatus() {
		return this.status;
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
				.append("id", this.id)
				.append("status", this.status)
				.append("name", this.name)
				.toString();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(-1683787035, -1518184133)
				.append(this.id)
				.append(this.status)
				.append(this.name)
				.toHashCode();
	}

	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Role)) {
			return false;
		}

		final Category rhs = (Category) o;

		return new EqualsBuilder()
				.append(this.id, rhs.id)
				.append(this.status, rhs.status)
				.append(this.name, rhs.name)
				.isEquals();
	}

	@Override
	public void setModificationDate(Date date) {
		this.modificationDate = date;
	}

	@Override
	public void setCreationDate(Date date) {
		this.creationDate = date;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setRecipes(Set<Recipe> recipes) {
		this.recipes = recipes;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setStatus(int status) {
		this.status = status;
	}
}
